#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include "config.h"
#include "FileControl.h"
#include <QFileDialog>
#include <QDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    

    SetUiContent();
    SetConnection();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::SetUiContent() {
    // 使用 QRegularExpressionValidator 限制输入为数字
    // QRegularExpression regex("^-?[0-9]*(\\.[0-9]*)?$"); // 只允许数字
    QRegularExpression regex("^\\d*(\\.\\d+)?$"); // 只允许正数
    QRegularExpressionValidator* validator = new QRegularExpressionValidator(regex, ui->sizeLineEdit);
    ui->sizeLineEdit->setValidator(validator);

    //设置文件夹选择的框的颜色 
    ui->selectLineEdit->setStyleSheet("QLabel { border: 2px solid #0000FF; }");


}
void MainWindow::SetConnection(){
    connect(ui->selectBtn,&QPushButton::clicked,this,&MainWindow::SelectFolder);
    connect(ui->act_About,&QAction::triggered,this,&MainWindow::actionAbout);
    connect(ui->confirmBtn,&QPushButton::clicked,this,&MainWindow::ConfirmClicked);
}

//select file or folder
void MainWindow::SelectFolder(){
    QString folderName = QFileDialog::getExistingDirectory(this, tr("Open File"),
                                                     "");
    if(!folderName.isEmpty()){
        ui->selectLineEdit->setText(folderName);
    }
}


/*toolBar action*/
void MainWindow::actionAbout(){
    QMessageBox* msgBox = new QMessageBox(this);
    msgBox->setText("Version:1.0.0");
    msgBox->setWindowTitle("about");
    msgBox->setIcon(QMessageBox::Information); // 设置图标类型
    msgBox->setStandardButtons(QMessageBox::Ok); // 自定义按钮

    msgBox->show();
}


void  MainWindow::ConfirmClicked() {
    std::cout << "confirm click";
    //整合筛选条件

    
    //开始查找相应的文件和文件夹
    QString path = ui->selectLineEdit->text();
    std::string filePath = path.toStdString();
    FILETYPE ft = TypeOfFile(filePath);
    if (ft == NONE || ft == NOTFOUND) 
    {
        //弹窗显示该路径不正确
        QMessageBox* msgBox = new QMessageBox(this);
        msgBox->setText("File Path is not right, select a path again");
        msgBox->setWindowTitle("warn");
        msgBox->setIcon(QMessageBox::Information); // 设置图标类型
        msgBox->setStandardButtons(QMessageBox::Ok); // 自定义按钮
        msgBox->show();

    }
}

